# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=libmsym
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.2.3
pkgrel=2
pkgdesc="Molecular point group symmetry lib (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/mcodev31/libmsym'
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
optdepends=("${MINGW_PACKAGE_PREFIX}-python: Python bindings")
source=("https://github.com/mcodev31/libmsym/archive/v${pkgver}-paper/${_realname}-${pkgver}.tar.gz"
        001-fix-install-destination.patch)
sha256sums=('3741ebe163cf40696570d6b62e4834ca587d43dcac9de713994cc5e2960fb8fd'
            '4cb1970d512f9c93f1c2e10ad435167c7a8d42cf403783197b61cf75530d4efd')

prepare() {
  cd ${_realname}-${pkgver}-paper
  patch -p1 -i "${srcdir}"/001-fix-install-destination.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  local pkgdir=$PWD/../pkg/${pkgname}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DINSTALL_CMAKE_DIR=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DMSYM_BUILD_PYTHON=ON \
      -DMSYM_PYTHON_INSTALL_OPTS="--root="${pkgdir}" --prefix=${MINGW_PREFIX}" \
      -S "${_realname}-${pkgver}-paper" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}-paper/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
